import SecureLS from 'secure-ls'
import { createStore, createLogger } from 'vuex'
import createPersistedState from 'vuex-persistedstate' // 持久化插件
import plugin from './plugin' //自定义插件
// 密码组件
const ls = new SecureLS({
    encodingType: 'aes',    //加密类型
    isCompression: false,
    encryptionSecret: 'encryption',   //PBKDF2值
})

// 导入所有模块js文件
const modulesFiles = import.meta.globEager('./modules/*.js')
// console.info('modulesFiles=====================')

// 装组模块组
// 导入所有 vuex 模块，自动加入namespaced:true，用于解决vuex命名冲突
const customModules = {}
Object.keys(modulesFiles).forEach(key => {
    let moduleKey = key.replace(/\.\/modules\/|\.js/g, '')
    customModules[moduleKey] = modulesFiles[key].default
    customModules[moduleKey].namespaced = true
})

// 插件
const customPlugins = [
    plugin,
    createPersistedState({
    // 指定存储到sessionStorage
        storage: window.sessionStorage,
        // storage: {
        //   getItem: (key) => ls.get(key),
        //   setItem: (key, value) => ls.set(key, value),
        //   removeItem: (key) => ls.remove(key),
        // }
        // key: "encryptionStore", //Key名称，默认vuex
        paths: ['menu', 'auth'], // 指定缓存 menu下的state的值
        getItem: key => ls.get(key),
        setItem: (key, value) => ls.set(key, value),
        removeItem: key => ls.remove(key)
    // filter: mutation => {
    //   //console.info(mutation);
    //   // 当调用auth/login方法时触发影响缓存值
    //   return mutation.type.indexOf("auth/login") !== -1;
    // }
    })
]
// 非生产环境添加日志
if (!import.meta.env.PROD) {
    customPlugins.push(createLogger())
}


export default createStore({
    state: {},
    mutations: {},
    actions: {},
    strict: true, // true 为严格使用mutations 来改变state的值
    modules: customModules,
    plugins: customPlugins
})
